#!/bin/bash

# 包含 definitions.sh
source ./definitions.sh
# 包含 functions.sh
source ./functions.sh

if [[ $EUID -eq 0 ]]; then
    : # echo "${green}当前 Shell 已经是 root 权限${reset}"
else
    echo "${red}当前 Shell 没有 root权限，请以 root 权限运行${reset}"
    exit 1
fi

# # 配置 redis
# serviceName="redis"
# echo "启动容器 $serviceName"
# docker-compose up -d $serviceName
# echo "⏳ 等待容器 $serviceName 变为 running 状态..."
# waitForContainerRunning $serviceName $waitTimeout $sleepTime
# if [[ $? -eq 1 ]]; then
#     echo "${yellow}等待超时：容器 $serviceName 在超时时间 ${waitTimeout}s 后仍未进入 running 状态${reset}"
#     exit 1
# fi
# echo "容器 ${green}$serviceName${reset} 已成功进入 ${green}running${reset} 状态"
# echo "开始配置 容器 $serviceName ..."
# 
# # 取出配置文件
# docker-compose cp ${serviceName}:/etc/redis/redis.conf redis.conf >/dev/null 2>&1
# if [[ $? -ne 0 ]]; then
#     echo "${red}配置容器 $serviceName 失败：从容器中复制配置文件失败 ${reset}"
#     exit 1
# fi
# # 修改配置文件
# setKeyValue ./redis-cfg/redis.conf port 6379
# setKeyValue ./redis-cfg/redis.conf bind 0.0.0.0
# setKeyValue ./redis-cfg/redis.conf maxmemory 4gb
# setKeyValue ./redis-cfg/redis.conf maxmemory-policy allkeys-lru
# setKeyValue ./redis-cfg/redis.conf requirepass nextcloud@redis
# setKeyValue ./redis-cfg/redis.conf timeout 0
# setKeyValue ./redis-cfg/redis.conf databases 16
#
# # 放入配置文件
# docker-compose exec ${serviceName} mkdir -p /etc/redis
# docker-compose cp redis.conf ${serviceName}:/etc/redis/redis.conf >/dev/null 2>&1
# if [[ $? -ne 0 ]]; then
#     # rm -f redis.conf
#     echo "${red}配置容器 $serviceName 失败：配置文件写入容器失败 ${reset}"
#     exit 1
# fi
# rm -f redis.conf
# docker-compose restart $serviceName
# echo "${green}配置容器 $serviceName 完成${reset}"

serviceName="nextcloud"
echo "启动容器 $serviceName"
docker-compose up -d $serviceName

echo "⏳ 等待容器 $serviceName 变为 running 状态..."
waitForContainerRunning $serviceName $waitTimeout $sleepTime
if [[ $? -eq 1 ]]; then
    echo "${yellow}等待超时：容器 $serviceName 在超时时间 ${waitTimeout}s 后仍未进入 running 状态${reset}"
    exit 1
fi
echo "容器 ${green}$serviceName${reset} 已成功进入 ${green}running${reset} 状态"
echo "配置容器 $serviceName SSL 以启用 HTTPS"
docker-compose exec nextcloud a2enmod ssl >/dev/null
if [[ $? -ne 0 ]]; then
    echo "${red}配置容器 $serviceName 失败：启用 SSL 失败 ${reset}"
    exit 1
fi
docker-compose exec nextcloud a2ensite default-ssl >/dev/null
if [[ $? -ne 0 ]]; then
    echo "${red}配置容器 $serviceName 失败：启用“网站默认 SSL 配置”失败 ${reset}"
    exit 1
fi
echo "重启容器 $serviceName"
docker-compose restart $serviceName

echo "⏳ 等待容器 $serviceName 变为 running 状态..."
waitForContainerRunning $serviceName $waitTimeout $sleepTime
if [[ $? -eq 1 ]]; then
    echo "${yellow}等待超时：容器 $serviceName 在超时时间 ${waitTimeout}s 后仍未进入 running 状态${reset}"
    exit 1
fi
echo "容器 ${green}$serviceName${reset} 已成功进入 ${green}running${reset} 状态"
echo "配置容器 $serviceName onlyoffice 插件"

tar -xf ./nextcloud-cfg/plugins/onlyoffice.tar.gz -C ./nextcloud-cfg/plugins
if [[ $? -ne 0 ]]; then
    echo "${red}配置容器 $serviceName 失败：解压 onlyoffice 插件失败${reset}"
    exit 1
fi
docker-compose cp ./nextcloud-cfg/plugins/onlyoffice ${serviceName}:/var/www/html/apps/onlyoffice >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
    rm -rf ./nextcloud-cfg/plugins/onlyoffice
    echo "${red}配置容器 $serviceName 失败：复制 onlyoffice 插件到容器失败${reset}"
    exit 1
fi
rm -rf ./nextcloud-cfg/plugins/onlyoffice
echo "${green}配置容器 $serviceName 完成${reset}"
